package org.softmed.neodatis.util.provider;

import java.io.File;

import org.neodatis.odb.Configuration;
import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBFactory;
import org.softmed.neodatis.util.PrimaryKeyManager;
import org.softmed.neodatis.util.trigger.TriggerEnvironment;

public class EmbeddedProvider implements ODBProvider {

	PrimaryKeyManager pkeyManager = new PrimaryKeyManager();
	TriggerEnvironment triggerEnvironment;

	String fileName;

	public EmbeddedProvider() {
	}

	public EmbeddedProvider(String fileName) {
		this.fileName = fileName;
	}

	@Override
	public ODB getODB() throws Throwable {
		ODB odb = ODBFactory.open(fileName);

		if (triggerEnvironment != null)
			return triggerEnvironment.setup(odb);

		return odb;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public TriggerEnvironment getTriggerEnvironment() {
		return triggerEnvironment;
	}

	public void setTriggerEnvironment(TriggerEnvironment triggerEnvironment) {
		this.triggerEnvironment = triggerEnvironment;
	}

	@Override
	public ODB getCleanODB() throws Throwable {
		return ODBFactory.open(fileName);
	}

	@Override
	public void refreshClassDefinitions() throws Throwable {
		Configuration.getCoreProvider().resetClassDefinitions();
	}

	@Override
	public void erase() throws Throwable {
		File file = new File(fileName);
		file.delete();
	}

	@Override
	public void restart() throws Throwable {
		// TODO Auto-generated method stub

	}

	@Override
	public void start() throws Throwable {
		// TODO Auto-generated method stub

	}

	@Override
	public void stop() throws Throwable {
		// TODO Auto-generated method stub

	}

	@Override
	public File getDatabaseFile() throws Throwable {
		return new File(fileName);
	}

	@Override
	public PrimaryKeyManager getPrimaryKeyManager() {
		return pkeyManager;
	}

}
